package com.xiao.controller;

import com.xiao.common.Constants;
import com.xiao.common.Result;
import com.xiao.common.dto.UserDto;
import com.xiao.server.AdminLoginServer;
import com.xiao.server.UserloginServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class LoginController<S> {

    @Autowired
    private UserloginServer userloginServer;

    @Autowired
    private AdminLoginServer adminLoginServer;

    @GetMapping("/book")
    public String book(){
        return "你好";
    }

    @PostMapping("/login")
    public Result userlogin(@RequestBody UserDto userDto){
        if (userDto.getUserId() == null || userDto.getPassword() == null){
            return Result.error(Constants.CODE_400,"请输入帐号和密码!");
        }
        if (userloginServer.login(userDto) != null){
            return Result.success(userDto);
        }else{
            return Result.error(Constants.CODE_400,"帐号或密码错误");
        }

    }

    @PostMapping("/adminlogin")
    public Result Adminlogin(@RequestBody UserDto userDto){
        if (userDto.getUserId() == null || userDto.getPassword() == null){
            return Result.error(Constants.CODE_400,"请输入帐号和密码!");
        }
        if (adminLoginServer.login(userDto) != null){
            return Result.success(userDto);
        }else{
            return Result.error(Constants.CODE_400,"帐号或密码错误");
        }

    }
}
